package com.linlong.linlongchat.controller;

import com.linlong.linlongchat.domain.po.Result;
import com.linlong.linlongchat.service.IMessageService;
import com.linlong.linlongchat.service.IRecordService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 *
 * @author yhj
 * @since 2024-09-19
 */
@RestController
@RequestMapping("/upload")
@Api(tags = "文件管理接口")
public class FileController {
    @Autowired
    private IMessageService messageService;
    @GetMapping("/uploadImage")
    public Result<String> uploadImage(MultipartFile file,String targetId)throws Exception {
        if (!file.isEmpty()) {
            String fileName = file.getOriginalFilename();
            String suffix = fileName.substring(fileName.lastIndexOf("."));
            String newFileName = System.currentTimeMillis() + suffix;
            String filePath = "D:/linlongchat/images/" + newFileName;
            file.transferTo(new java.io.File(filePath));
            if(messageService.insertMessage(targetId, filePath)){
                return Result.ok(filePath);
            }
            return Result.fail("上传失败");
        } else {
            return Result.fail("上传失败");
        }
    }
}
